package com.xjrsoft.module.visual.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.visual.dto.SaveVisualDto;
import com.xjrsoft.module.visual.dto.VisualDto;
import com.xjrsoft.module.visual.dto.VisualListDto;
import com.xjrsoft.module.visual.entity.*;
import com.xjrsoft.module.visual.mapper.XjrVisualMapper;
import com.xjrsoft.module.visual.service.*;
import com.xjrsoft.module.visual.vo.VisualVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 可视化表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-12-19
 */
@Service
//@AllArgsConstructor
public class XjrVisualServiceImpl extends ServiceImpl<XjrVisualMapper, XjrVisual> implements IXjrVisualService {

    @Autowired
    private  IXjrVisualConfigService visualConfigService;

    @Autowired
    private  IXjrVisualReleaseService visualReleaseService;

    @Autowired
    private  IXjrVisualReleaseRelationService releaseRelationService;

    @Autowired
    private  IXjrVisualHomeRelationService homeRelationService;


    @Override
    public boolean save(VisualDto dto) {
        XjrVisual visual = BeanUtil.copy(dto, XjrVisual.class);
        return this.save(visual);
    }

    @Override
    @Transactional
    public boolean updateVisualDetail(String id, XjrVisual visual, XjrVisualConfig visualConfig) {
        visual.setId(id);
        // config更新
        return this.updateById(visual) && visualConfigService.updateById(visualConfig);
    }

    @Override
    public PageOutput<VisualVo> selectPageData(VisualListDto dto) {
        QueryWrapper<XjrVisual> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(!StringUtil.isEmpty(dto.getCategory()), XjrVisual::getCategory, dto.getCategory());
        return ConventPage.getPageOutput(this.page(ConventPage.getPage(dto), queryWrapper), VisualVo.class);
    }

    @Override
    public Map<String, Object> saveVisual(SaveVisualDto dto) {
        String visualId = IdWorker.get32UUID();
        XjrVisual visual = BeanUtil.copy(dto.getVisual(), XjrVisual.class);
        visual.setId(visualId);
        visual.setStatus(0); //默认未发布
        visual.setIsDeleted(0); // 默认未删除
        // config新增
        XjrVisualConfig visualConfig = BeanUtil.copy(dto.getConfig(), XjrVisualConfig.class);
        visualConfig.setId(StringUtil.randomUUID());
        visualConfig.setVisualId(visualId);
        boolean saveVisual = this.save(visual);
        boolean saveVisualConfig = visualConfigService.save(visualConfig);

        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("id",visualId);

        return resultMap;
    }

    @Override
    public boolean isReleased(String id) {
        XjrVisual visual = this.getById(id);
        return Integer.valueOf(1).equals(visual.getStatus());
    }

    @Override
    public boolean updateById(XjrVisual visual) {
        if (Integer.valueOf(0).equals(visual.getStatus())) {
            String visualId = visual.getId();
            XjrVisual oldVisual = this.getById(visualId);
            if (oldVisual.getStatus() == 1) {
                // 改成未发布，已发布的首页
                XjrVisualRelease visualRelease = visualReleaseService.getOne(Wrappers.<XjrVisualRelease>query().lambda()
                        .eq(XjrVisualRelease::getKanBanId, visualId).eq(XjrVisualRelease::getIndexs, 1), false);
                if (visualRelease != null) {
                    String releaseId = visualRelease.getId();
                    // 修改发布成首页的BI
                    XjrVisualRelease newRelease = new XjrVisualRelease();
                    newRelease.setId(releaseId);
                    newRelease.setIndexs(0);
                    visualReleaseService.updateById(newRelease);
                    //删除发布的首页
                    releaseRelationService.remove(Wrappers.<XjrVisualReleaseRelation>query().lambda()
                            .eq(XjrVisualReleaseRelation::getVisualReleaseId, releaseId));
                    // 删除设置的首页
                    homeRelationService.remove(Wrappers.<XjrVisualHomeRelation>query().lambda()
                            .eq(XjrVisualHomeRelation::getVisualReleaseId, releaseId));
                }
            }
        }
        return super.updateById(visual);
    }
}
